<?php
    $this->layout('base.phtml', [
        'title' => _('Log in'),
        'canonical' => url_full('login'),
        'current_page' => 'login',
        'back_options' => [
            'reset' => true,
            'track' => false,
        ],
        'has_errors' => $form->isInvalid(),
        'modal_enabled' => true,
    ]);
?>

<div class="section section--small">
    <div class="section__title">
        <h1><?= _('Log in') ?></h1>
    </div>

    <?php if ($status === 'user_deleted'): ?>
        <?= $this->include('alerts/_success.phtml', ['message' => _('Your account and your data are now deleted.')]) ?>
    <?php endif; ?>

    <form method="post" action="<?= url('create session') ?>">
        <?= $this->include('alerts/_error.phtml', ['message' => $form->error('@base')]) ?>

        <div class="form-group">
            <label for="email">
                <?= _('Email address') ?>
            </label>

            <?php if ($form->isInvalid('email')): ?>
                <p id="email-error" class="form-group__error">
                    <?= _('Error:') ?>
                    <?= $form->error('email') ?>
                </p>
            <?php endif; ?>

            <input
                id="email"
                name="email"
                type="email"
                value="<?= protect($form->email) ?>"
                required
                autofocus
                autocomplete="username"
                <?php if ($form->isInvalid('email')): ?>
                    aria-errormessage="email-error"
                    aria-invalid="true"
                <?php endif; ?>
            />
        </div>

        <div class="form-group">
            <label for="current-password">
                <?= _('Password') ?>
            </label>

            <?php if ($form->isInvalid('password')): ?>
                <p id="password-error" class="form-group__error">
                    <?= _('Error:') ?>
                    <?= $form->error('password') ?>
                </p>
            <?php endif; ?>

            <div class="form-group__stack" data-controller="input-password">
                <input
                    id="current-password"
                    name="password"
                    type="password"
                    value=""
                    required
                    data-input-password-target="input"
                    autocomplete="current-password"
                    <?php if ($form->isInvalid('password')): ?>
                        aria-errormessage="password-error"
                        aria-invalid="true"
                    <?php endif; ?>
                />

                <button
                    type="button"
                    class="no-mobile js-only"
                    data-action="input-password#toggle"
                    data-input-password-target="button"
                    aria-label="<?= _('Show password as plain text. Note: this will visually expose your password.') ?>"
                >
                    <?= icon('eye') ?>
                    <?= _('Show') ?>
                </button>
            </div>
        </div>

        <div class="form__actions">
            <button type="submit" class="button--primary">
                <?= _('Login') ?>
            </button>
        </div>

        <input type="hidden" name="csrf_token" value="<?= csrf_token($form) ?>" />
        <input type="hidden" name="redirect_to" value="<?= protect($form->redirect_to) ?>" />
    </form>

    <?php if (!get_app_configuration('demo')): ?>
        <p class="paragraph--centered">
            <a href="<?= url('forgot password') ?>">
                <?= _('Forgot password?') ?>
            </a>
        </p>
    <?php endif; ?>

    <?php if (get_app_configuration('registrations_opened')): ?>
        <p class="paragraph--centered">
            <a href="<?= url('registration') ?>">
                <?= _('No account yet?') ?>
            </a>
        </p>
    <?php endif; ?>
</div>
